<script type="text/javascript">
    $(function(){
        var input;
        var er = $('.errorMessage');
        if(er.length>=0)
        {
            for(var i=0;i<er.length;i++)
            {
                if($(er[i]).css('display') !='none')
                {
                    input = $(er[i]).prev();
                    input.popover({content:er.text(),placement:'right'}).popover('show');
                }
            }
            er.remove();
        }

        $('a#imageDownload').click(function(e){
            var articleImage = $('input#Article_image');
            articleImage.click();
        });
        $('input#Article_image').change(function(e){
            $('input#imageField').val($(this).val());
        });
    });
        function beforeValidate(form)
        {
            return true;
        }
    function beforeValidateAttribute(form, attribute)
    {
        return true;
    }

    function afterValidateAttribute(form, attribute, data, hasError)
    {
            var erMess = data[attribute.id];
           if(hasError)
           {
               if(attribute.id === 'Article_content')
               {
                   //console.log("1");
                   $('#'+attribute.id).parent().popover('destroy');
                   $('#'+attribute.id).prev().css({'border-color':'red','color':'red'});
                   $('#'+attribute.id).parent().popover({content:erMess,placement:'right'}).popover('show');


               }
               if(attribute === 'Article_content')
               {
                   console.log("2")
                   $('#'+attribute).parent().popover({trigger:'manual',content:data[attribute],placement:'right'}).bind('blur').popover('show');
               }

               if(attribute.id !== 'Article_content')
               {
                   //console.log("3");
                   $('#'+attribute.id).popover('destroy');
                   $('#'+attribute.id).popover({trigger:'manual',content:erMess,placement:'right'}).popover('show');
               }
               return false;
           }
           else{
               //console.log("4");

               if(attribute.id === 'Article_content')
               {
                   $('#'+attribute.id).parent().popover('hide');
                   $('#'+attribute.id).prev().css('border-color','#9E9E9E');
               }
               if(attribute.id !== 'Article_content')
               {
                   $('#'+attribute.id).popover('hide');
               }

               return true;
           }
     }
        function afterValidate(form,data,hasError)
        {
           // $('.errorMessage').remove();
            if(hasError)
            {
                for(attribute in data)
                {

                    if(attribute === 'Article_content'){
                        $('#'+attribute).prev().css('border-color','red');
                        $('#'+attribute).parent().popover({trigger:'manual',content:data[attribute],placement:'right'}).bind('blur').popover('show');
                    }
                    if(attribute !== 'Article_content')
                    {
                        $('#'+attribute).popover({trigger:'manual',content:data[attribute],placement:'right'}).bind('blur').popover('show');
                    }
                }
                return false;
            }
            return true;
        }
</script>
<?php
/* @var $this BackendController */
/* @var $model Article */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('TActiveForm', array(
	'id'=>'article-form',
    'htmlOptions' => array('enctype' => 'multipart/form-data'),

    // Please note: When you enable ajax validation, make sure the corresponding
	// controller action is handling ajax validation correctly.
	// There is a call to performAjaxValidation() commented in generated controller code.
	// See class documentation of CActiveForm for details on this.
	'enableAjaxValidation'=>true,
    'enableClientValidation'=>true,
    'clientOptions'=>array(
        'beforeValidate'=>'js:beforeValidate',
        'afterValidate'=>'js:afterValidate',
        'validateOnSubmit'=>true,
        'validateOnType'=>true,
        'validateOnChange'=>true,
        'hideErrorMessage'=>true
    ),

)); ?>
    <div class="alert alert-info">
        <?php echo Yii::t('ArticleModule.base','Fields with')?><span class="required"> * </span> <?php echo Yii::t('ArticleModule.base','are required.')?>
    </div>

    <div class="well well-sm col-lg-12">
        <?php echo $form->labelEx($model,'category_id',array('class'=>'paddingLeft10')); ?>
        <?php echo $form->dropDownListTb($model,'category_id',$model->getCategories()); ?>
        <?php echo $form->error($model,'category_id',array('afterValidateAttribute'=>'js:afterValidateAttribute')); ?>
        <span class="well-into">
            <?php echo $form->labelEx($model,'mid',array('class'=>'paddingLeft10')); ?>
            <?php echo $form->dropDownListTb($model,'mid',$model->getMenuOptions()); ?>
            <?php echo $form->error($model,'mid',array('afterValidateAttribute'=>'js:afterValidateAttribute')); ?>
        </span>
        <span class="well-into">
            <?php echo $form->labelEx($model,'status',array('class'=>'paddingLeft10')); ?>
            <?php echo $form->dropDownListTb($model,'status',$model->getStatusOptions()); ?>
            <?php echo $form->error($model,'status',array('afterValidateAttribute'=>'js:afterValidateAttribute')); ?>
        </span>
    </div>

    <div class="input-group" >
        <?php echo $form->labelEx($model,'image',array('class'=>'input-group-btn paddingLeftRight6020')); ?>
        <?php echo $form->fileField($model,'image',array('style'=>'display:none')); ?>
        <?php echo CHtml::openTag('span',array('class'=>'input-group-btn'));?>
        <?php echo CHtml::openTag('a',array('id'=>'imageDownload','class'=>'btn btn-primary'));?>
        <?php echo CHtml::tag('i',array('class'=>'glyphicon glyphicon-plus'),'')?>
        <?php echo Yii::t('ArticleModule.base','Browse')?>
        <?php echo CHtml::closeTag('a')?>
        <?php echo CHtml::closeTag('span');?>
        <?php echo $form->textField($model,'image',array('id'=>'imageField','class'=>'form-control','readonly'=>'readonly','disabled'=>'disabled','style'=>'width:300px')); ?>
        <?php echo $form->error($model,'image',array('afterValidateAttribute'=>'js:afterValidateAttribute')); ?>
    </div>


	<div class="input-group">
		<?php echo $form->labelTb($model,'title'); ?>
		<?php echo $form->textFieldTb($model,'title',array('size'=>96,'maxlength'=>255)); ?>
		<?php echo $form->error($model,'title',array('afterValidateAttribute'=>'js:afterValidateAttribute')); ?>
	</div>

    <div class="input-group">
        <?php echo $form->labelTb($model,'description'); ?>
        <?php echo $form->textAreaTb($model,'description',array('rows'=>3, 'cols'=>95)); ?>
        <?php echo $form->error($model,'description',array('afterValidateAttribute'=>'js:afterValidateAttribute')); ?>
    </div>

	<div class="input-group">
		<?php echo $form->labelTb($model,'content',array('style'=>'width:90px')); ?>
		<?php echo $form->textAreaEditor($model,'content',array('id'=>'Article_content','width'=>800,'height'=>700)); ?>
		<?php echo $form->error($model,'content',array('afterValidateAttribute'=>'js:afterValidateAttribute')); ?>
	</div>

    <div class="input-group">
        <?php echo $form->labelTb($model,'tags',array('style'=>'width:90px')); ?>
        <?php echo $form->textAreaTb($model,'tags',array('rows'=>1, 'cols'=>95)); ?>
        <?php echo $form->error($model,'tags',array('afterValidateAttribute'=>'js:afterValidateAttribute')); ?>
    </div>

	<div>
		<?php echo $form::submitButtonTb($model,array('class'=>'btn btn-success','glyphicon'=>'floppy-save')); ?>
	</div>

<?php $this->endWidget();?>

</div><!-- form -->